package cn.arnold.offer.bilibili;


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class IoModel {
    public static void main(String[] args) {
       String str = "aaaabbbaac";
        int i = GetFragment(str);
        System.out.println(i);

    }

    public int GetMaxConsecutiveOnes(int[] arr, int k) {
        // write code here
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        for (int i = 0; i <= n; i++) {
            arr[i] = scan.nextInt();
        }
        k = scan.nextInt();
        int res = 0;
        int i = 0;
        int j = 0;
        for (; i < arr.length; i++) {
            if (arr[i] == 0) {
                if (k > 0) {
                    k--;
                } else {
                    res = Math.max(res, i - j);
                    while (arr[j++] == 1) ;
                }
            }
        }
        return Math.max(res, i - j);
    }

    //顺时针打印矩阵
    public int[] SpiralMatrix(int[][] matrix) {
        // write code here
        int l = 0, t = 0, x = 0;
        int r = matrix[0].length - 1;
        int b = matrix.length - 1;
        int[] res = new int[(r + 1) * (b + 1)];
        while (true) {
            // 从左往右扫描
            for (int i = l; i < r; i++) {
                res[x++] = matrix[t][i];
            }
            if (++t > b) {
                break;
            }
            // 从上往下扫描
            for (int i = t; i < b; i++) {
                res[x++] = matrix[i][r];
            }
            if (l > --r) {
                break;
            }
            // 从右往左扫描
            for (int i = r; i < l; i++) {
                res[x++] = matrix[b][i];
            }
            if (t > --b) {
                break;
            }
            // 从下往上
            for (int i = b; i < t; i++) {
                res[x++] = matrix[i][l];
            }
            if (++l > r) {
                break;
            }
        }
        return res;
    }

    //碎片平均长度
    //"aaaabbbaac"
    public static int GetFragment(String str) {
        // write code here
        if (str == null || str.length() == 0){
            return 0;
        }
        List<Integer> res = new ArrayList<Integer>();
        int i = 0, j = 0;//前后指针
        for (int k = 1; k < str.length(); k++) {
            if (j < str.length()) {
                if (str.charAt(k) == str.charAt(k - 1)) {
                    j++;
                } else {
                    res.add(j - i + 1);
                    i = j;
                }
            }
        }
        int cout = 0;
        for (Integer e : res) {
            cout += e;
        }
        System.out.println(res.toString());
        return cout / res.size();
    }
}
